<template>
  <div class="goods-comment">
    <div class="head">
      <div class="data">
        <p>
          <span>{{ info.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ info.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            href="javascript:;"
            :class="{ active: tagName === item.title }"
            @click="tagClick(item)"
            v-for="(item, index) in info.tags"
            :key="index"
            >{{ item.title }}（{{ item.tagCount }}）</a
          >
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a
        @click="sortClick(null)"
        href="javascript:;"
        :class="{ active: commentParams.sortField === null }"
        >默认</a
      >
      <a
        @click="sortClick('createTime')"
        href="javascript:;"
        :class="{ active: commentParams.sortField === 'createTime' }"
        >最新</a
      >
      <a
        @click="sortClick('praiseCount')"
        href="javascript:;"
        :class="{ active: commentParams.sortField === 'praiseCount' }"
        >最热</a
      >
    </div>
    <!-- 评论列表 -->
    <!-- 列表 -->
    <div class="list">
      <div class="item" v-for="item in commentList" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ filterName(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i
              class="iconfont icon-wjx01"
              v-for="i in item.score"
              :key="i + 'a'"
            ></i>
            <i
              class="iconfont icon-wjx02"
              v-for="i in 5 - item.score"
              :key="i + 'b'"
            ></i>
            <!-- <span class="attr">{{ filterAttrs(item.orderInfo.specs) }}</span> -->
            <span class="attr">{{ item.attr }}</span>
          </div>
          <div class="text">
            {{ item.content }}
          </div>
          <GoodsCommentImage :pictures="item.pictures"></GoodsCommentImage>
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"
              ><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span
            >
          </div>
        </div>
      </div>
    </div>
    <!-- 分页组件 -->
    <XtxPagination
      @pageChange="pageChange"
      :page="commentParams.page"
      :pagesize="commentParams.pageSize"
      :total="total"
    ></XtxPagination>
  </div>
</template>
<script>
import { goodsEvaluate, goodsEvaluatePage } from '@/api/product'
import { inject, ref, reactive, watch } from 'vue'
import GoodsCommentImage from './goods-comment-image.vue'
export default {
  name: 'GoodsComment',
  components: {
    GoodsCommentImage
  },
  setup (props, { emit }) {
    const goods = inject('goods') // 响应式数据  返回值.value才是我们想要的值
    const info = ref('')
    goodsEvaluate(goods.value.id).then(res => {
      console.log('条件：', res)
      res.result.tags = [
        {
          title: '全部评价',
          tagCount: res.result.evaluateCount
        },
        {
          title: '有图',
          tagCount: res.result.hasPictureCount
        },
        ...res.result.tags
      ]
      info.value = res.result

      emit('updateNum', res.result.evaluateCount)
    })

    // 筛选条件处理
    const commentParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: null, // 是否有图
      tag: null, // 当前选中的属性tag
      sortField: null, // [praiseCount,createTime]
      sortMethod: 'desc'
    })
    const tagName = ref('全部评价')
    function tagClick (item) {
      // 按钮状态跟随点击变化
      tagName.value = item.title
      switch (item.title) {
        case '全部评价':
          commentParams.hasPicture = null
          commentParams.tag = null
          break
        case '有图':
          commentParams.hasPicture = true
          commentParams.tag = null
          break
        default:
          commentParams.hasPicture = null
          commentParams.tag = item.title
          break
      }
      commentParams.page = 1
    }
    // 排序 点击事件
    function sortClick (str) {
      commentParams.sortField = str
      commentParams.page = 1
    }
    // 调用评论列表接口获取数据并渲染
    const commentList = ref([])
    const total = ref(100)
    async function getData () {
      console.log('goods.id', goods)
      const res = await goodsEvaluatePage(goods.value.id, commentParams)
      console.log('评论列表:', res)
      total.value = res.result.counts
      res.result.items.forEach(item => {
        // item.attr = item.orderInfo.specs
        //   .map(i => {
        //     return i.name + ':' + i.nameValue
        //   })
        //   .join(' ')
        item.attr = item.orderInfo.specs.reduce((d, i) => {
          return d + i.name + ':' + i.nameValue + ' '
        }, '')
      })
      commentList.value = res.result.items
    }
    // getData()

    // 属性转换
    function filterAttrs (arr) {
      return arr
        .map(item => {
          return item.name + ':' + item.nameValue
        })
        .join(' ')
    }
    // 名字脱免
    function filterName (str) {
      // 只有二个字，张三==》张**
      // 三个或者三个以上  张大山==》张**山
      if (str.length <= 2) {
        return str.slice(0, 1) + '**'
      } else {
        return str.slice(0, 1) + '**' + str.slice(-1)
      }
    }
    // 页码改变时
    function pageChange (page) {
      commentParams.page = page
      // getData()
    }
    // 评论列表的接口条件改变就立马调用接口
    watch(
      commentParams,
      () => {
        getData()
      },
      {
        immediate: true
      }
    )

    return {
      info,
      tagClick,
      tagName,
      commentParams,
      commentList,
      filterAttrs,
      filterName,
      pageChange,
      total,
      sortClick
    }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
.list {
  padding: 0 20px;
  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;
    .user {
      width: 160px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }
      span {
        padding-left: 10px;
        color: #666;
      }
    }
    .body {
      flex: 1;
      .score {
        line-height: 40px;
        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }
        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }
    .text {
      color: #666;
      line-height: 24px;
    }
    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}
</style>
